package yu.com.telecontroller;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import android.app.Service;  
import android.os.Vibrator; 
import android.widget.Toast;

public class telecontrollerActivity extends Activity {
    /** Called when the activity is first created. */
	private String TAG = "tvcontroller";
	private final int MSG_PLAYCODE = 1;
	private final int MSG_DECODE = 2;
	private ImageButton mOnOff;
	private ImageButton mMute;
	private ImageButton mNumber1;
	private ImageButton mNumber2;
	private ImageButton mNumber3;
	private ImageButton mNumber4;
	private ImageButton mNumber5;
	private ImageButton mNumber6;
	private ImageButton mNumber7;
	private ImageButton mNumber8;
	private ImageButton mNumber9;
	private ImageButton mNumber0;
	private ImageButton mInput;
	private ImageButton mBack;
	private ImageButton mNumbervolup;
	private ImageButton mNumbervoldown;
	private ImageButton mMenu;
	private ImageButton mNumberprev;
	private ImageButton mNumbernext;	
	private Button		mTest;
	private Toast 		 mToast = null;
	private Vibrator m_Vib;
	private SoundPool mSoundPool = null;
	private HashMap <Integer, Integer> mPoolIDMap = null;   
	private Handler mHandler;
	private String mFileName = "tv"; 
	private int mMaxplay = 5;
	private int [] mCodeVolup = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0x8a,0x22,0xaa,0x8a,0x88,0x88,0x80}; 
	private int [] mCodeVoldown = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0x8a,0x28,0xaa,0x8a,0x28,0x88,0x80};
	private int [] mCodeInput = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0x88,0x8a,0x28,0xaa,0x8a,0x28,0x80};
	private int [] mCodePowon = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xa2,0x2a,0x8a,0xa2,0x22,0x28,0x80};
	private int [] mCodeMute = 		{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xa8,0xaa,0xa2,0xaa,0x88,0x88,0x80};	
	
	private int [] mCodeMenu = 		{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0x81,0x22,0xaa,0x8a,0x88,0x88,0x80}; 
	private int [] mCodePrev = 		{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0x82,0x28,0xaa,0x8a,0x28,0x88,0x80};
	private int [] mCodeNext = 		{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0x83,0x8a,0x28,0xaa,0x8a,0x28,0x80};
	private int [] mCodeNumber0 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xa4,0x2a,0x8a,0xa2,0x22,0x28,0x80};
	private int [] mCodeNumber1 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xa5,0xaa,0xa2,0xaa,0x88,0x88,0x80};	
	private int [] mCodeNumber2 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xa6,0x2a,0x8a,0xa2,0x22,0x28,0x80};
	private int [] mCodeNumber3 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xa7,0xaa,0xa2,0xaa,0x88,0x88,0x80};
	private int [] mCodeNumber4 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xa8,0x2a,0x8a,0xa2,0x22,0x28,0x80};
	private int [] mCodeNumber5 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xa9,0xaa,0xa2,0xaa,0x88,0x88,0x80};
	private int [] mCodeNumber6 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xaa,0x2a,0x8a,0xa2,0x22,0x28,0x80};
	private int [] mCodeNumber7 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xab,0xaa,0xa2,0xaa,0x88,0x88,0x80};
	private int [] mCodeNumber8 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xac,0x2a,0x8a,0xa2,0x22,0x28,0x80};
	private int [] mCodeNumber9 = 	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xad,0xaa,0xa2,0xaa,0x88,0x88,0x80};
	private int [] mCodeBack =  	{0x12,0x11,0x38,0xff,0xff,0x00,0xa8,0xaa,0xa2,0x28,0x88,0x88,0xae,0xaa,0xa2,0xaa,0x88,0x88,0x80};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.tv);
//		Intent intent = new Intent(this,TVActivity.class);
//		intent.setAction(Intent.ACTION_VIEW);
//		startActivity(intent);
        mOnOff = (ImageButton)findViewById(R.id.button_on);
        mMute = (ImageButton)findViewById(R.id.button_mute);
        mNumber1 = (ImageButton)findViewById(R.id.ImageButton1);
        mNumber2 = (ImageButton)findViewById(R.id.ImageButton2);
        mNumber3 = (ImageButton)findViewById(R.id.ImageButton3);
        mNumber4 = (ImageButton)findViewById(R.id.ImageButton4);
        mNumber5 = (ImageButton)findViewById(R.id.ImageButton5);
        mNumber6 = (ImageButton)findViewById(R.id.ImageButton6);
        mNumber7 = (ImageButton)findViewById(R.id.ImageButton7);
        mNumber8 = (ImageButton)findViewById(R.id.ImageButton8);
        mNumber9 = (ImageButton)findViewById(R.id.ImageButton9);
        mNumber0 = (ImageButton)findViewById(R.id.ImageButton0);
        mInput = (ImageButton)findViewById(R.id.ImageButton_);
        mBack = (ImageButton)findViewById(R.id.ImageButton_back);
        mNumbervolup = (ImageButton)findViewById(R.id.ImageButton_add);
        mNumbervoldown = (ImageButton)findViewById(R.id.ImageButton_reduce);
        mNumberprev = (ImageButton)findViewById(R.id.ImageButton_prev);
        mNumbernext = (ImageButton)findViewById(R.id.ImageButton_next);
        mMenu = (ImageButton)findViewById(R.id.ImageButton_menu);
        mTest = (Button)findViewById(R.id.button_test);
        
        mOnOff.setVisibility(View.VISIBLE);
        mMute.setVisibility(View.VISIBLE);
        mNumber1.setVisibility(View.VISIBLE);
        mNumber2.setVisibility(View.VISIBLE);
        mNumber3.setVisibility(View.VISIBLE);
        mNumber4.setVisibility(View.VISIBLE);
        mNumber5.setVisibility(View.VISIBLE);
        mNumber6.setVisibility(View.VISIBLE);
        mNumber7.setVisibility(View.VISIBLE);
        mNumber8.setVisibility(View.VISIBLE);
        mNumber9.setVisibility(View.VISIBLE);
        mNumber0.setVisibility(View.VISIBLE);
        mInput.setVisibility(View.VISIBLE);
        mBack.setVisibility(View.VISIBLE);        
        mNumbervolup.setVisibility(View.VISIBLE);
        mNumbervoldown.setVisibility(View.VISIBLE);
        mNumberprev.setVisibility(View.VISIBLE);
        mNumbernext.setVisibility(View.VISIBLE);
        mMenu.setVisibility(View.VISIBLE);
     
		SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
		mMaxplay = prefs.getInt("max", 4);
		Log.i(TAG,"Maxplay=" + mMaxplay);
        final SoundPool mSoundPool = new SoundPool(mMaxplay, AudioManager.STREAM_MUSIC,0); 
        mPoolIDMap = new HashMap<Integer, Integer>();   
        mPoolIDMap.put(0, mSoundPool.load(getBaseContext(),R.raw.num_0,1));
        mPoolIDMap.put(1, mSoundPool.load(getBaseContext(),R.raw.num_1,1));
        mPoolIDMap.put(2, mSoundPool.load(getBaseContext(),R.raw.num_2,1));
        mPoolIDMap.put(3, mSoundPool.load(getBaseContext(),R.raw.num_3,1));
        mPoolIDMap.put(4, mSoundPool.load(getBaseContext(),R.raw.num_4,1));
        mPoolIDMap.put(5, mSoundPool.load(getBaseContext(),R.raw.num_5,1));
        mPoolIDMap.put(6, mSoundPool.load(getBaseContext(),R.raw.num_6,1));
        mPoolIDMap.put(7, mSoundPool.load(getBaseContext(),R.raw.num_7,1));
        mPoolIDMap.put(8, mSoundPool.load(getBaseContext(),R.raw.num_8,1));
        mPoolIDMap.put(9, mSoundPool.load(getBaseContext(),R.raw.num_9,1));
        mPoolIDMap.put(10, mSoundPool.load(getBaseContext(),R.raw.num_a,1));
        mPoolIDMap.put(11, mSoundPool.load(getBaseContext(),R.raw.num_b,1));
        mPoolIDMap.put(12, mSoundPool.load(getBaseContext(),R.raw.num_c,1));
        mPoolIDMap.put(13, mSoundPool.load(this,R.raw.num_d,1));
        mPoolIDMap.put(14, mSoundPool.load(this,R.raw.num_e,1));
        mPoolIDMap.put(15, mSoundPool.load(this,R.raw.num_f,1));

        //mPoolIDMap.put(16, mSoundPool.load(this,R.raw.number1,0));
        
 
        final AudioManager mAudioManage = (AudioManager) this.getSystemService(   
        		Context.AUDIO_SERVICE);  
    	
        m_Vib = (Vibrator) this.getSystemService(Service.VIBRATOR_SERVICE);  
       

        mHandler = new Handler(){
        	public void handleMessage(Message msg){
        		int [] play = (int[])msg.obj;
        		//SoundPool mSound = (SoundPool)msg.obj;
        		switch(msg.what)
        		{
        		case MSG_PLAYCODE:

        	        int streamVolume = mAudioManage.getStreamVolume(AudioManager.STREAM_MUSIC);  
        	    	for(int i = 0; i < play.length;i++)
        	    	{
        	    		int code = play[i];
        	        	int p1 = (code >> 4) & 0x0f;
        	        	int p2 = code & 0xf;  
        	        	if(p1 == p2)
        	        		mSoundPool.play(mPoolIDMap.get(p1), streamVolume, streamVolume, 1, 1, 1f);
        	        	else
        	        	{
        	        		mSoundPool.play(mPoolIDMap.get(p1),streamVolume,streamVolume, 0, 1, 1f);
        	        		mSoundPool.play(mPoolIDMap.get(p2),streamVolume,streamVolume, 0, 1, 1f);
        	        	}
        	    	}
        			break;
        		case MSG_DECODE:
        			break;
        		}
        	}
        };
        mTest.setText("test |" + mMaxplay);
        mTest.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
      	        int streamVolume = mAudioManage.getStreamVolume(AudioManager.STREAM_MUSIC);  
    	    	for(int i = 0; i < mPoolIDMap.size();i++)
    	    	{
    	        	mSoundPool.play(mPoolIDMap.get(i), streamVolume, streamVolume, 1, 0, 1f);
    	    	}		
			}
		});
        mOnOff.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodePowon);
				msg.sendToTarget();
				TipButton("power on");
			}
		});
        mMute.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeMute);
				msg.sendToTarget();				
				TipButton("mute");
			}
		});
        mNumber1.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber1);
				msg.sendToTarget();
				TipButton("number 1");
			}
		});
        mNumber2.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber2);
				msg.sendToTarget();
				TipButton(" number 2");
			}
		});
        mNumber3.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber3);
				msg.sendToTarget();
				TipButton("number 3");
			}
		});
        mNumber4.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber4);
				msg.sendToTarget();
				TipButton("number 4");
			}
		});
        mNumber5.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber5);
				msg.sendToTarget();
				TipButton("number 5");
			}
		});
        mNumber6.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber6);
				msg.sendToTarget();
				TipButton("number 6");
			}
		});
        mNumber7.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber7);
				msg.sendToTarget();
				TipButton("number 7");
			}
		});
        mNumber8.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber8);
				msg.sendToTarget();
				TipButton("number 8");
			}
		});
        mNumber9.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber9);
				msg.sendToTarget();
				TipButton("number 9");
			}
		});
        mNumber0.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNumber0);
				msg.sendToTarget();
				TipButton("number 0");
			}
		});
        mNumbervolup.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeVolup);
//				msg.sendToTarget();		
//				TipButton("vol up");
				mMaxplay++;
				Log.i(TAG, "maxpaly=" + mMaxplay);
				SaveState();
				mTest.setText("test |" + mMaxplay);
			}
		});
        mInput.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeInput);
				msg.sendToTarget();	
				TipButton("input");
			}
		});
        mBack.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeBack);
				msg.sendToTarget();
				TipButton("back");
			}
		});        
        mNumbervoldown.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeVoldown);
//				msg.sendToTarget();		
//				TipButton("vol down");
				if(mMaxplay > 0)
					mMaxplay--;
				Log.i(TAG, "maxpaly=" + mMaxplay);
				SaveState();
				mTest.setText("test |" + mMaxplay);
			}
		});
        mNumberprev.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodePrev);
				msg.sendToTarget();
				TipButton("prev");
			}
		});
        mNumbernext.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeNext);
				msg.sendToTarget();
				TipButton("next");
			}
		});
        mMenu.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(MSG_PLAYCODE, mCodeMenu);
				msg.sendToTarget();
				TipButton("menu");
			}
		});
        
    }
    public void TipButton(String tip)
    {
//		m_Vib.vibrate(50);
//		if(mToast != null)
//			mToast.cancel();
//		mToast = Toast.makeText(this, tip, Toast.LENGTH_SHORT); 
//		mToast.show();      	
    }
    public void SaveState()
    {
		if (mFileName != null) {
			
			SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
			SharedPreferences.Editor edit = prefs.edit();
			edit.putInt("max", mMaxplay);
			edit.commit();
		}	
    }
    @Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		SaveState();
	}
 }